//
//  MTHomeDouYinCell.swift
//  MT
//
//  Created by ooops on 2020/6/1.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import ZFPlayer

public typealias MTHomeDouYinCellPlayBack = ((UIButton) -> Void)

class MTHomeDouYinCell: UICollectionViewCell {
    
    var playOrPauseBack: MTHomeDouYinCellPlayBack?
    
    /// 展示模型
    var model: HomeListDetailModel? {
        didSet {
            guard let model = model else { return }
            /// 封面图
            if let urlString = model.imgs?.first?.href {
                coverImageView.kf.setImage(with: URL(string: urlString), placeholder: UIImage(named: "placeholder-rectangle"))
            }
        }
    }
    
    
    class func reuseIdentity() -> String {
        return "MTHomeDouYinCellID"
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.tag = 8800
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        imgView.clipsToBounds = true
        imgView.tag = 100
        return imgView
    }()
    
    /// 视频暂停 / 开始按钮
    lazy var pauseIconButtonLeft: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_start"), for: .normal)
        btn.setImage(UIImage(named: "icon_stop-1"), for: .selected)
        btn.addTarget(self, action: #selector(MTHomeDouYinCell.playVideo(sender:)), for: .touchUpInside)
        return btn
    }()
    
    @objc private func playVideo(sender: UIButton) {
        self.playOrPauseBack?(sender)
    }
    
    
    private func setupUI() {
        contentView.addSubview(coverImageView)
        coverImageView.addSubview(pauseIconButtonLeft)
//        contentView.addSubview(<#T##view: UIView##UIView#>)
//        contentView.addSubview(<#T##view: UIView##UIView#>)
//        contentView.addSubview(<#T##view: UIView##UIView#>)
    }
    
    private func makeConstraint() {
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        pauseIconButtonLeft.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
}
